package com.tepth.remote.control.delegate.launcher;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.ServiceUtils;
import com.evernote.android.job.JobManager;
import com.tepth.remote.control.R;
import com.tepth.remote.control.base.config.SpKey;
import com.tepth.remote.control.base.mvp.BaseMvpDelegate;
import com.tepth.remote.control.delegate.home.HomeDelegate;
import com.tepth.remote.control.service.ControlService;
import com.tepth.remote.control.service.LineStatus;
import com.tepth.remote.control.service.alarm.ControlAlarmReceiver;
import com.tepth.remote.control.service.job.ControlCheckJobCreator;
import com.tepth.remote.core.app.Control;
import com.tepth.remote.core.utils.storage.ControlPreference;
import com.tepth.remote.ui.alert.DialogLoader;
import com.tepth.remote.ui.alert.IDialogClickListener;

/**
 * Description:
 *
 * @author Hequn.Lee
 * @date 2018/11/8
 */
public class LauncherDelegate extends BaseMvpDelegate<LauncherPresenter> implements LauncherContract.View, LauncherContract.NeedPermissionCallback {

    private String imei = "";
    private DialogLoader dialogLoader;

    @Override
    public Object setLayout() {
        return R.layout.delegate_luncher;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        dialogLoader = new DialogLoader(getProxyActivity());
        //重置状态为初始化
        ControlPreference.addCustomAppProfile(SpKey.KEY_ONLINE_STATUS, LineStatus.INIT_STATUS);
        readStateWithCode(this);
    }

    @Override
    public void loginSuccess() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //开启前台服务
            final Intent intent = new Intent(getProxyActivity(), ControlService.class);
            Control.getApplicationContext().startForegroundService(intent);
        } else {
            ServiceUtils.startService(ControlService.class);
        }
        startWithPop(new HomeDelegate());
        final Intent intent = new Intent();
        intent.setAction(ControlAlarmReceiver.ALARM_ACTION);
        final AlarmManager alarmManager = Control.getAlarmManager();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
                    15 * 60 * 1000,
                    PendingIntent.getBroadcast(getProxyActivity(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT));
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            alarmManager.setExact(AlarmManager.RTC_WAKEUP,
                    15 * 60 * 1000,
                    PendingIntent.getBroadcast(getProxyActivity(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT));
        } else {
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                    15 * 60 * 1000, 15 * 60 * 1000,
                    PendingIntent.getBroadcast(getProxyActivity(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT));
        }
        JobManager.create(Control.getApplicationContext()).addJobCreator(new ControlCheckJobCreator());
    }

    @Override
    public void loginFailed(int code, String msg) {
        dialogLoader.showFailed("提示", code == 500 ? ("系统中未找到您的手机串号：" + imei + "，请重试") : "网络出错，请重试", new IDialogClickListener() {
            @Override
            public void onClick() {
                dialogLoader.clearDialog();
                mPresenter.getConfig(imei);
            }
        }, new IDialogClickListener() {
            @Override
            public void onClick() {
                AppUtils.exitApp();
            }
        });
    }

    @Override
    public LauncherPresenter getPresenter() {
        return new LauncherPresenter(this);
    }

    @Override
    public void showLoading(String str) {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onNeedPermissionSuccess(String imei) {
        this.imei = imei;
        //"867119028842714"
        mPresenter.getConfig(imei);
    }

    @Override
    public void onNeedPermissionFailed() {
        if (dialogLoader != null) {
            dialogLoader.showFailed("提示", "禁止读取手机状态将导致本软件无法使用，请允许授权", new IDialogClickListener() {
                @Override
                public void onClick() {
                    dialogLoader.clearDialog();
                    readStateWithCode(null);
                }
            });
        }
    }
}
